const express = require('express')
const msgRouter = require("./router/msg")
const app=express()

const cors=require('cors'); 
app.use(cors())
const server = require('http').createServer(app);
const io=require('socket.io')(server);

io.use(cors({
  origin: '*',
}));

app.use(express.urlencoded({ extended: false })) 
app.use(express.json())

app.use(function (req, res, next) {
  res.cc = function (data, status = 100) {
    res.send({
      status,
      data:data.data,
      message: data.message||'成功',
    })
  }
  next()
})

app.use('/msg',msgRouter)
// app.listen(3000, () => {
//   console.log('http://127.0.0.1:3000')
// })
server.listen(9090, () => {
  console.log('socketio 启动了--------------------------');
});
io.on('connection', ws => { 
  console.log('有人链接了',ws.id);
  ws.id = Date.now()
  ws.on("message", message => {
    ws.send(`[${ws.id}]: ${message}`);
  });

});